package cn.doitedu.spark

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{Dataset, SparkSession}

case class ObjectFileCase(a:String,b:Int)

object OjbectFileDemo {

  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("")
      .master("local")
      .getOrCreate()
    import spark.implicits._

    val ds: RDD[ObjectFileCase] = spark.createDataset(Seq(ObjectFileCase("a", 3), ObjectFileCase("b", 4))).rdd

    ds.saveAsObjectFile("d:/sparkobj")

    val rdd: RDD[ObjectFileCase] = spark.sparkContext.objectFile[ObjectFileCase]("d:/sparkobj")
    rdd.foreach(println)


    val rdd2 = spark.sparkContext.objectFile[ObjectFileCase]("d:/sparkobj").map(o => (o.a, o.b))
    rdd2.foreach(println)

    spark.close()
  }

}
